package com.zhongya.example.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi

class LinearGradientView : View {

    val mPaint = Paint()

    constructor(context: Context) : super(context) {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        mPaint.shader = LinearGradient(
            0f,
            height/2f,
            width.toFloat()/2,
            height/2f,
            0xffff0000.toInt(),
            0xff00ff00.toInt(),
            Shader.TileMode.CLAMP
        )

        /*LinearGradient(0f, height/2f, width.toFloat(), height/2f,
            intArrayOf(-0xda2947, -0x7fff01, -0xda2947), floatArrayOf(0f, 1f, 0f), Shader.TileMode.MIRROR)
*/


        canvas?.drawRect(0f,0f,width.toFloat(),height.toFloat(), mPaint)

    }
}